package cn.tedu._05notice.controller;

import cn.tedu._05notice.base.response.JsonResult;
import cn.tedu._05notice.base.response.StatusCode;
import cn.tedu._05notice.mapper.NoticeMapper;
import cn.tedu._05notice.pojo.dto.NoticeAddParam;
import cn.tedu._05notice.pojo.dto.NoticeSelectParam;
import cn.tedu._05notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._05notice.pojo.entity.Notice;


import cn.tedu._05notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._05notice.pojo.vo.NoticeSelectVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/**
 * @RestController 组合注解
 * 1  组合注解，等价于@Controller+@ResponseBody
 * 2  在类上添加注解后，会为当前类的所有方法添加@ResponseBody注解
 */
/**
 Slf4j注解:
 *   1.Lombok提供的日志注解;
 *   2.添加在类上,会为该类创建一个日志对象log,可以使用日志对象log的方法来打印日志;
 *     log.trace("xxx");
 *     log.debug("xxx");
 *     log.info("xxx");
 *     log.warn("xxx");
 *     log.error("xxx");
 */

/**
 * knife4j 配置注解
 * Tag（name=“”）给类名添加中文描述
 *  @Operation(summary = "方法名称") 给方法添加中文描述
 *  @ApiOperationSupport(order = 1)给方法排序，数字越小优先级越高
 */
@Slf4j
@RestController
@Tag(name="资讯管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    //自动装配
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 添加信息
     */

    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    public JsonResult addNotice(@RequestBody NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam:"+noticeAddParam);
        //终极目标：将咨询数据存入数据表notice中
        Notice notice=new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);
        //处理其他属性
        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        log.debug("notice:"+notice);
        //调用接口方法
        noticeMapper.insertNotice(notice);
        return  JsonResult.OK();
    }

    /**
     * 查询咨询列表
     */
    @GetMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult noticeSelectOVs(NoticeSelectParam noticeSelectParam){
        log.debug("noticeSelectParam:"+noticeSelectParam);
List<NoticeSelectVO> noticeSelectVOs=noticeMapper.selectNotice(noticeSelectParam);
        return JsonResult.OK(noticeSelectVOs);
    }

    // 查询咨询详情
    @GetMapping("detail")
    @Operation(summary = "资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {
        @Parameter(name = "id",description = "资讯编号",required = true),
        @Parameter(name="username",description = "用户名",required = true,hidden = true)
    })
    public JsonResult detail(Long id ,String username){
        log.debug("id:"+id);
        log.debug("noticeMapper:"+noticeMapper.noticeDetailInfoVO(id));
        return  JsonResult.OK(noticeMapper.noticeDetailInfoVO(id));
    }


    //删除咨询
    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult del(Long id){
        log.debug("id:"+id);
        int rows=noticeMapper.deleteNoticeById(id);
        if(rows>0){
            return JsonResult.OK();
        }
            return new JsonResult(StatusCode.OPERATION_FAILED);

    }
    //更新咨询
    @PutMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 50)
    @Parameters(value={
       @Parameter(name="id"),
       @Parameter(name="title"),
       @Parameter(name="content"),
       @Parameter(name="type"),
       @Parameter(name="status"),
       @Parameter(name="noticeUpdateParam",hidden = true)
    })
    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam:"+noticeUpdateParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        int rows=noticeMapper.updateNoticeById(notice);
        if (rows>0){
            return JsonResult.OK();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

}
